package com.tongren.framework.maintabfragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qmuiteam.qmui.util.QMUIResHelper;
import com.qmuiteam.qmui.widget.QMUITabSegment;
import com.tongren.framework.R;
import com.tongren.framework.constants.ConstantValues;
import com.tongren.framework.core.BaseFragment;
import com.tongren.framework.core.ITabFragment;
import com.tongren.framework.fragment.NewsList2Fragment;
import com.tongren.framework.fragment.NewsList3Fragment;
import com.tongren.framework.fragment.NewsListFragment;
import com.tongren.framework.maintabfragment.adapter.MyFragmentAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends BaseFragment implements ITabFragment,View.OnClickListener{

    private QMUITabSegment mTabSegment;
    private ViewPager mContentViewPager;
    private List<Fragment> fragments;
    private MyFragmentAdapter myFragmentAdapter;
    private Map<ContentPage, View> mPageMap = new HashMap<>();
    private ContentPage mDestPage = ContentPage.Item1;
//    private QDItemDescription mQDItemDescription;

    public static MainFragment newInstance(int page) {
        MainFragment fragment = new MainFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(ConstantValues.KEY_FRAGMENT_PAGE, page);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        enableLazyLoad();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_main, container, false);
    }
    @Override
    public void setUpView(View view) {
        mTabSegment = (QMUITabSegment) view.findViewById(R.id.tabSegment);
        mContentViewPager = (ViewPager) view.findViewById(R.id.contentViewPager);
    }

    @Override
    public void setUpData() {
        fragments = new ArrayList<Fragment>();
        fragments.add(new NewsListFragment());
        fragments.add(new NewsList2Fragment());
        fragments.add(new NewsList3Fragment());
        initTabAndPager();
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onMenuItemClick() {

    }

    @Override
    public BaseFragment getFragment() {
            return this;
    }

    private PagerAdapter mPagerAdapter = new PagerAdapter() {
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public int getCount() {
            return ContentPage.SIZE;
        }

        @Override
        public Object instantiateItem(final ViewGroup container, int position) {
            ContentPage page = ContentPage.getPage(position);
            View view = getPageView(page);
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            container.addView(view, params);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    };

    private void initTabAndPager() {
        mContentViewPager.setAdapter(new MyFragmentAdapter(this.getFragmentManager(), fragments));
        mContentViewPager.setCurrentItem(mDestPage.getPosition(), false);
        QMUITabSegment.Tab component3 = new QMUITabSegment.Tab("时政要闻" );
        component3.setTextColor(QMUIResHelper.getAttrColor(getContext(), R.attr.qmui_config_color_blue),
                QMUIResHelper.getAttrColor(getContext(), R.attr.qmui_config_color_red));
        mTabSegment.addTab(component3);
        mTabSegment.addTab(new QMUITabSegment.Tab("今日要闻"));
        mTabSegment.addTab(new QMUITabSegment.Tab("通知通告"));
        mTabSegment.setupWithViewPager(mContentViewPager, false);
        mTabSegment.setMode(QMUITabSegment.MODE_FIXED);
        mTabSegment.setHasIndicator(true);
        mTabSegment.setIndicatorPosition(false);
        mTabSegment.setIndicatorWidthAdjustContent(true);
        mTabSegment.addOnTabSelectedListener(new QMUITabSegment.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int index) {
                mTabSegment.hideSignCountView(index);
            }

            @Override
            public void onTabUnselected(int index) {

            }

            @Override
            public void onTabReselected(int index) {
                mTabSegment.hideSignCountView(index);
            }

            @Override
            public void onDoubleTap(int index) {

            }
        });
    }

    private View getPageView(ContentPage page) {
        View view = mPageMap.get(page);
        if (view == null) {
            TextView textView = new TextView(getContext());
            textView.setGravity(Gravity.CENTER);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
            textView.setTextColor(ContextCompat.getColor(getContext(), R.color.main_color));
            textView.setText("这是第 " + (page.getPosition() + 1) + " 个 Item 的内容区");
            view = textView;
            mPageMap.put(page, view);
        }
        return view;
    }

    public enum ContentPage {
        Item1(0),
        Item2(1),
        Item3(2);

        public static final int SIZE = 3;
        private final int position;

        ContentPage(int pos) {
            position = pos;
        }

        public static ContentPage getPage(int position) {
            switch (position) {
                case 0:
                    return Item1;
                case 1:
                    return Item2;
                case 2:
                    return Item3;
                default:
                    return Item1;
            }
        }

        public int getPosition() {
            return position;
        }
    }
}
